package com.wondersgroup.fjzlpt.console.service.impl.yljgjfsb;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.remoteResult.ResultObject;
import com.quick.framework.util.ExcelOrWordUtils;
import com.quick.framework.util.FormatDate;
import com.wondersgroup.fjzlpt.console.dao.inter.yljgjfsb.JfsbDAO;
import com.wondersgroup.fjzlpt.console.service.inter.yljgjfsb.JfsbxxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 机房信息服务层
 * <p>
 * <p>
 * Created by sanma on 2017/8/14.
 */
@Service
public class JfsbxxServiceImpl implements JfsbxxService {

    @Autowired
    private JfsbDAO jfsbDAO;

    @Override
    public JSONObject queryPageJfxx(Map map) {
        return jfsbDAO.queryPageJfxx(map);
    }

    @Override
    public JSONArray querySbxxById(Map map) {
        return jfsbDAO.querySbxxById(map);
    }

    @Override
    public JSONObject saveJfxx(Map map) {
        ResultObject ro = new ResultObject();
        try {
            String type = map.remove("type").toString();
            if("update".equals(type)){
                jfsbDAO.updateJfxx(map);
            }else if ("insert".equals(type)){
                jfsbDAO.addJfxx(map);
            }else if ("delete".equals(type)) {
                jfsbDAO.deleteJfxx(map);
            }
        }catch (Exception e ) {
            e.printStackTrace();
            ro.changeFaultState(e.getMessage());
        }
        return (JSONObject) JSON.toJSON(ro);
    }

    @Override
    public JSONObject saveSbxx(Map map) {
        ResultObject ro = new ResultObject();
        try {
            String type = map.remove("type").toString();
            Date date = FormatDate.getDateY_M(map.get("GMRQ").toString());
            map.put("GMRQ" , date);
            if("insert".equals(type)){
                jfsbDAO.addSbxx(map);
            }else if ("update".equals(type)){
                jfsbDAO.updateSbxx(map);
            }else if ("delete".equals(type)) {
                jfsbDAO.deleteSbxx(map);
            }
        }catch (Exception e ) {
            e.printStackTrace();
            ro.changeFaultState(e.getMessage());
        }
        return (JSONObject) JSON.toJSON(ro);
    }

    /**
     * 将机房信息导出到Excel
     * @param map
     * @param request
     * @param response
     */
    public void downloadToExcel(Map map, HttpServletRequest request, HttpServletResponse response){
        JSONArray dataArr = jfsbDAO.queryAllForExcel(map);
        //表头
        StringBuffer titleStr = new StringBuffer();
        titleStr.append("{'JGMC':'机构名称','KSMC':'科室名称','JFMC':'机房名称','LXMC':'类型','SBBH':'设备编号','SBMC':'设备名称',");
        titleStr.append("'CD':'产地','SCCJ':'生产厂家','SBXH':'设备型号','GMRQ':'购买日期','XJQK':'新旧情况','GMDJ':'购买单价','SYQK':'使用情况'}");
        JSONObject title = JSONObject.parseObject(titleStr.toString());
        dataArr.add(0, title);

        //排列循序
        List<String> titleField = new ArrayList<>();
        titleField.add("JGMC");
        titleField.add("KSMC");
        titleField.add("JFMC");
        titleField.add("LXMC");
        titleField.add("SBBH");
        titleField.add("SBMC");
        titleField.add("CD");
        titleField.add("SCCJ");
        titleField.add("SBXH");
        titleField.add("GMRQ");
        titleField.add("XJQK");
        titleField.add("GMDJ");
        titleField.add("SYQK");

        ExcelOrWordUtils.downLoadExcelFile(dataArr, titleField, "机房设备列表", request, response);
    }
}
